package com.dream.demoquickstart.controller;

import com.dream.demoquickstart.pojo.Article;
import com.dream.demoquickstart.pojo.ArticleCategory;
import com.dream.demoquickstart.pojo.ArticleState;
import com.dream.demoquickstart.service.ArticleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 1.0
 * @program: demo-quickstart
 * @className ArticleController
 * @description:
 * @author: Administrator
 * @create: 2025-08-09 15:42
 **/
@RestController // 标记为控制器类
@RequestMapping("/article") // 定义请求路径
@CrossOrigin // 允许跨域请求
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * 新增文章接口
     * @param article 文章对象
     * @return 操作结果消息
     */
    @PostMapping
    public String insertArticle(@RequestBody Article article) {
        articleService.insertArticle(article);
        return "新增文章成功";
    }

    /**
     * 条件分页列表查询接口
     * @param pageNum 当前页码
     * @param pageSize 每页数量
     * @param categoryId 分类ID (可选)
     * @param stateId 状态ID (可选)
     * @return 分页后的文章数据
     */
    @GetMapping
    public PageInfo<Article> selectAllArticle(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String stateId
    ) {
        return articleService.selectAllArticle(pageNum, pageSize, categoryId, stateId);
    }

    /**
     * 根据ID查询文章详情接口
     * @param id 文章ID
     * @return 文章对象
     */
    @GetMapping("/{id}")
    public Article selectArticleById(@PathVariable Integer id) {
        return articleService.selectArticleById(id);
    }

    /**
     * 更新文章接口
     * @param article 文章对象
     * @return 操作结果消息
     */
    @PutMapping("/updateArticle")
    public String updateArticle(@RequestBody Article article) {
        articleService.updateArticle(article);
        return "更新文章成功";
    }

    /**
     * 根据ID删除文章接口
     * @param id 文章ID
     * @return 操作结果消息
     */
    @DeleteMapping("/{id}")
    public String deleteArticleById(@PathVariable Integer id) {
        articleService.deleteArticleById(id);
        return "删除文章成功";
    }

    /**
     * 获取所有文章分类
     * @return 分类列表
     */
    @GetMapping("/selectAllCategory")
    public List<ArticleCategory> selectAllCategory() {
        return articleService.selectAllCategory();
    }

    /**
     * 获取所有文章状态
     * @return 状态列表
     */
    @GetMapping("/selectAllState")
    public List<ArticleState> selectAllState() {
        return articleService.selectAllState();
    }
}